package com.mida.dms.knw.service.impl;

import com.mida.dms.knw.mapper.KnwNoumenonRuleMapper;
import com.mida.dms.knw.Result.BaseResult;
import com.mida.dms.knw.entity.KnwNoumenonRule;
import com.mida.dms.knw.service.KnwNoumenonRuleService;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <h1>本体规则服务实现</h1>
 * <p>
 *
 * @author jian.li on 2021/2/25 10:50
 */
@Service
@Slf4j
public class KnwNoumenonRuleServiceImpl extends BaseResult implements KnwNoumenonRuleService {

    @Autowired
    KnwNoumenonRuleMapper knwNoumenonRuleMapper;

//    //ID生成器
//    @Autowired
//    @Qualifier("oIDGanerator")
//    private OIDGanerator oIDGanerator;

//    @Override
//    @Transactional
//    public ActionResult add(KnwNoumenonRule knwNoumenonRule) {
//        if(knwNoumenonRule.getNoumenonRuleSoid() != null){
//            log.info(knwNoumenonRule.toString() + "不是一个新增,转到更新");
//            return update(knwNoumenonRule);
//        }
//        log.info("开始新增一个本体规则: " + knwNoumenonRule.toString());
//        StopWatch stopWatch = StopWatch.createStarted();
//        //返回结果
//        ActionResult actionResult = new ActionResult();
//        try{
//            if(knwNoumenonRule.getNoumenonSoid() == null){
//                throw new RuntimeException("本体SOID不能为空");
//            }
//            if(knwNoumenonRule.getDerailCodingSystemSoid() == null){
//                throw new RuntimeException("本体规则编码体系SOID不能为空");
//            }
//            knwNoumenonRule.setCreateDate(new Date());
//            knwNoumenonRule.setUpdateDate(new Date());
//            Long noumenonRuleSoid = oIDGanerator.getSOID();
//            knwNoumenonRule.setNoumenonRuleSoid(noumenonRuleSoid);
//            knwNoumenonRuleMapper.insert(knwNoumenonRule);
//            buildActionSuccess(actionResult);
//        }catch (Exception e){
//            log.error("新增本体规则发送异常: " + e);
//            buildActionWarn(actionResult,e);
//        }finally {
//            stopWatch.stop();
//            log.info("本次新增本体规则耗时: " + stopWatch.getTime() + "ms");
//        }
//        return actionResult;
//    }

//    @Override
//    @Transactional
//    public ActionResult update(KnwNoumenonRule knwNoumenonRule) {
//        log.info("开始更新一个本体规则: "+ knwNoumenonRule.toString());
//        StopWatch stopWatch = StopWatch.createStarted();
//        //返回结果
//        ActionResult actionResult = new ActionResult();
//        try{
//            //根据本体soid查询出的本体规则
//            KnwNoumenonRule knr = knwNoumenonRuleMapper.findByKnwNoumenonSoid(knwNoumenonRule.getNoumenonSoid());
//            if(knr == null){
//                throw new RuntimeException("本体规则不存在: " + knwNoumenonRule.getNoumenonRuleSoid());
//            }
//            if(knr.getNoumenonRuleSoid().longValue() != knwNoumenonRule.getNoumenonRuleSoid().longValue()){
//                throw new RuntimeException("本体绑定的本体规则与传入参数不一致");
//            }
////            knwNoumenonRule.setWbCode(CharTransFormUtil.toWubi(knwNoumenonRule.getNoumenonRuleName()));
////            knwNoumenonRule.setPyCode(CharTransFormUtil.toPinyinSimple(knwNoumenonRule.getNoumenonRuleName()));
//            knwNoumenonRule.setUpdateDate(new Date());
//            knwNoumenonRuleMapper.updateByPrimaryKeySelective(knwNoumenonRule);
//            buildActionSuccess(actionResult);
//        }catch(Exception e){
//            log.error("更新本体规则发生异常: ",e);
//            buildActionWarn(actionResult,e);
//        }finally {
//            stopWatch.stop();
//            log.info("本次更新耗时: " + stopWatch.getTime() + "ms");
//        }
//        return actionResult;
//    }

//    @Override
//    public ActionResult delete(Long soid) {
//        log.info("开始删除一个本体规则: " + soid);
//        StopWatch stopWatch = StopWatch.createStarted();
//        ActionResult actionResult = new ActionResult();
//        try{
//            knwNoumenonRuleMapper.deleteByPrimaryKey(soid);
//            buildActionSuccess(actionResult);
//        }catch (Exception e){
//            log.error("删除本体规则发送异常",e);
//            buildActionWarn(actionResult,e);
//        }finally {
//            stopWatch.stop();
//            log.info("本次删除耗时: " + stopWatch.getTime() + "ms");
//        }
//        return actionResult;
//    }

    @Override
    public ListResult<KnwNoumenonRule> getByKnwNoumenonSoid(long soid) {
        log.info("开始根据本体soid查询本体规则: " + soid);
        StopWatch stopWatch = StopWatch.createStarted();
        ListResult listResult = new ListResult();
        try {
            KnwNoumenonRule knwNoumenonRule = knwNoumenonRuleMapper.findByKnwNoumenonSoid(soid);
            buildListSuccess(listResult,knwNoumenonRule);
        }catch (Exception e){
            log.error("根据本体soid查询本体规则发送异常",e);
            buildListWarn(listResult,"根据本体soid查询本体规则失败");
        }finally {
            stopWatch.stop();
            log.info("本次查询耗时: " + stopWatch.getTime() + "ms");
        }
        return listResult;
    }

}
